package com.algo.iplibrary;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class IpRegion {
	private String minProvincialLevelId = "000000000000000000000009";
	private String maxProvincialLevelId = "000000000000000000000042";
	
	private String ipRegionFile;
	private String storageSystem = "local";
	
	private Map<String, String> ipRegion = new HashMap<String, String>();
	
	/*public void setStorageSystem(String storageSystemType) {
		if (storageSystemType.toLowerCase().equals("hdfs")){
			storageSystem = "hdfs";
		}
	}*/
	
	public IpRegion(String ipRegionFile, String storageSystemType) {
		this.ipRegionFile = ipRegionFile;
		if (storageSystemType.toLowerCase().equals("hdfs")){
			storageSystem = "hdfs";
		}
		try {
			init();
		} catch (NumberFormatException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

	}
	
	private void init() throws NumberFormatException, IOException {
		if (storageSystem.equals("local")) {
			initIpRegionLocal();
		} else {
			initIpRegionHDFS();
		}
			
	}
	
	public String getProvincialID(String regionID) {
		if(regionID.compareTo(maxProvincialLevelId)<= 0  && regionID.compareTo(minProvincialLevelId) >= 0) {
			return regionID;
		}  else {
			while ( regionID != null &&
					!(regionID.compareTo(maxProvincialLevelId)<= 0  
					&& regionID.compareTo(minProvincialLevelId) >= 0)
				) {
				regionID = ipRegion.get(regionID);
			}
		}
		return regionID;
		
	}
	
	private void initIpRegionLocal() throws NumberFormatException, IOException {
		BufferedReader br;
		br = new BufferedReader(new FileReader(ipRegionFile));
		String str;

		while ((str = br.readLine()) != null) {
			String[] items = str.split("\\,");
			if (items.length == 3) {
				ipRegion.put(items[0], items[1]);
			}
		}
		br.close();
	}
	
	private void initIpRegionHDFS() throws NumberFormatException, IOException {
		FileSystem fs = FileSystem.get(URI.create(ipRegionFile),new Configuration());
		FSDataInputStream fin = fs.open(new Path(ipRegionFile));
		BufferedReader br = new BufferedReader(new InputStreamReader(fin));
		String str;

		while ((str = br.readLine()) != null) {
			String[] items = str.split("\\,");
			if (items.length == 3) {
				ipRegion.put(items[0], items[1]);
			}
		}
		br.close();
	}
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		IpLibrary ipl = new IpLibrary(
				"E:\\IPlib-Region-000000000000000000000008-top100-20110728171818", "local");
		//ipl.init();
		IpRegion ir= new IpRegion("E:\\Region-000000000000000000000008-top100-new", "local");
		//ir.init();
		System.out.println(ipl.getIPLocation("112.117.1.1"));
		System.out.println(ir.getProvincialID(ipl.getIPLocation("112.117.1.1")));
	}
}
